package com.example.apiproject.service.impl;

import com.example.apiproject.entity.Colleges;
import com.example.apiproject.dao.CollegesDao;
import com.example.apiproject.service.CollegesService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (Colleges)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@Service("collegesService")
public class CollegesServiceImpl implements CollegesService {
    @Resource
    private CollegesDao collegesDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Colleges queryById(Integer id) {
        return this.collegesDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param colleges 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Colleges> queryByPage(Colleges colleges, PageRequest pageRequest) {
        long total = this.collegesDao.count(colleges);
        return new PageImpl<>(this.collegesDao.queryAllByLimit(colleges, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param colleges 实例对象
     * @return 实例对象
     */
    @Override
    public Colleges insert(Colleges colleges) {
        this.collegesDao.insert(colleges);
        return colleges;
    }

    /**
     * 修改数据
     *
     * @param colleges 实例对象
     * @return 实例对象
     */
    @Override
    public Colleges update(Colleges colleges) {
        this.collegesDao.update(colleges);
        return this.queryById(colleges.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.collegesDao.deleteById(id) > 0;
    }
}
